package thinker.miracle.commons;

import java.io.Serializable;
import java.util.HashMap;

/**
 * 页面返回结果 
 * 
 * @author bo.chen
 *
 */
public class Result extends HashMap<String,Object> implements Serializable{
	
	private static final long serialVersionUID = 2134095854409651387L;
	
	/**
	 * 返回结果状态 000为成功
	 */
	protected final static String SUCCESS = "000";

	/**
	 * 返回结果状态998为失败 （默认值）
	 */
	protected final static String DEFEAT_998 = "998";
	
	/**
	 * tocken验证不通过
	 */
	protected final static String DEFEAT_999 = "999";

	/**
	 * 默认  提供code和msg
	 * 
	 * @author bo.chen
	 *
	 */
	private enum RESULT {
		code,msg
	};
	
	public Result(){
		super();
		defeat();//默认设置为失败
	}
	
	/**
	 * 默认设置返回结果为失败
	 * 
	 * @return 当前对象
	 */
	public Result defeat(){
		return setCode(DEFEAT_998).setMsg("");
	}
	
	/**
	 * 设置返回结果为成功
	 * 
	 * @return 当前对象
	 */
	public Result success(){
		this.put(RESULT.code.toString(), SUCCESS);
		return this;
	}
	
	/**
	 * 设置返回编码
	 * 
	 * 默认：000：成功，001，异常，999：tocken验证失败
	 * 
	 * @param code
	 * 
	 * @return 当前对象
	 */
	public Result setCode(String code){
		this.put(RESULT.code.toString(), code);
		return this;
	}
	
	/**
	 * 设置返回信息
	 * 
	 * @param msg
	 * 
	 * @return 当前对象
	 */
	public Result setMsg (String msg){
		this.put(RESULT.msg.toString(), msg);
		return this;
	}
	
	/**
	 * 增加返回结果
	 * 
	 * @param key
	 * @param value
	 * 
	 * @return 当前对象
	 */
	public Result addData(String key,Object value){
		if(key != null)
			this.put(key, value);
		return this;
	}
	
	/**
	 * tocken验证失败返回对象 （通用）
	 * 
	 * @return	result对象
	 */
	public static Result tockenVerifiedFailed(){
		return new Result()
				.setCode(Result.DEFEAT_999)
				.setMsg("tocken verify failed");
	}
}
